/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.bean;

import client.dal.AccountDAL;
import client.dal.WorkerDAL;
import client.entity.Account;
import client.entity.Worker;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import util.EncryptionPassword;

/**
 *
 * @author mrlucky
 */
@ManagedBean
@SessionScoped
public class WorkerEditInfo {

    private Worker worker;
    private Account acc;
    private String old_pass_temp;
    private String new_pass_temp;
    private String confirm_new_pass_temp;
    private String gender_temp;
    private String pass_temp;
    private int accId ;

    public int getAccId() {
        return accId;
    }

    public void setAccId(int accId) {
        this.accId = accId;
    }
    

    public String getPass_temp() {
        return pass_temp;
    }

    public void setPass_temp(String pass_temp) {
        this.pass_temp = pass_temp;
    }

    public String getGender_temp() {
        return gender_temp;
    }

    public void setGender_temp(String gender_temp) {
        this.gender_temp = gender_temp;
    }

    public String getNew_pass_temp() {
        return new_pass_temp;
    }

    public void setNew_pass_temp(String new_pass_temp) {
        this.new_pass_temp = new_pass_temp;
    }

    public String getConfirm_new_pass_temp() {
        return confirm_new_pass_temp;
    }

    public void setConfirm_new_pass_temp(String confirm_new_pass_temp) {
        this.confirm_new_pass_temp = confirm_new_pass_temp;
    }

    public String getOld_pass_temp() {
        return old_pass_temp;
    }

    public void setOld_pass_temp(String old_pass_temp) {
        this.old_pass_temp = old_pass_temp;
    }

    public Worker getWorker() {
        if (worker == null) {
            Account ac = (Account) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("account");
            accId = ac.getId();
            worker = new WorkerDAL().loadWorkerByAccountID(accId);
            acc = new AccountDAL().loadAccountByID(accId);
            if (worker.getGender()) {
                gender_temp = "1";
            } else {
                gender_temp = "0";
            }
        }
        return worker;
    }

    public void setWorker(Worker worker) {
        this.worker = worker;
    }

    public Account getAcc() {
        if (acc == null) {
            acc = new Account();
        }
        return acc;
    }

    public void setAcc(Account acc) {
        this.acc = acc;
    }

    /**
     * Creates a new instance of WorkerEditInfo
     */
    public WorkerEditInfo() {
    }

    public String editAccountInfo() {
        String pass = new EncryptionPassword().Decryption(acc.getPassword());
        if (old_pass_temp.equals(pass)) {
            acc.setPassword(new EncryptionPassword().Encryption(new_pass_temp));
            new AccountDAL().updateAccount(acc);
            HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            ss.removeAttribute("workerProfile");
            return "workerProfile.xhtml?faces-redirect=true";
        } else {
            FacesContext.getCurrentInstance().addMessage("form1:pass", new FacesMessage("* Password is invalid"));
            return "";
        }
    }

    public String editWorkerInfo() {
        boolean gen = false;
        if (gender_temp.equals("1")) {
            gen = true;
        }
        worker.setName(worker.getName());
        worker.setGender(gen);
        worker.setPhone(worker.getPhone());
        worker.setAddress(worker.getAddress());
        worker.setEmail(worker.getEmail());
        new WorkerDAL().updateWorker(worker);
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("workerProfile");
        return "workerProfile.xhtml?faces-redirect=true";
    }
    
    public String backToProfile()
    {
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("workerProfile");
        return "workerProfile.xhtml?faces-redirect=true";
    }       
    
    public String getParam(String param) {

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);
    }
           
}
